
package cz.muni.fi.pa165.carpark.servicies;

import cz.muni.fi.pa165.carpark.EmployeePosition;
import cz.muni.fi.pa165.carpark.TO.EmployeeTO;
import cz.muni.fi.pa165.carpark.dao.EmployeeDAO;
import cz.muni.fi.pa165.carpark.entities.Employee;
import java.util.LinkedList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.convert.ConversionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author xmarton1
 */
@Service
public class EmployeeServiceImpl implements EmployeeService{
    
    @Autowired
    private EmployeeDAO employeeDAO;
    
    @Autowired
    @Qualifier("TOconverter")
    private ConversionService converter;

    @PreAuthorize("hasRole('employee_manager') or hasRole('superadmin')")
    @Override
    @Transactional
    public void createEmp(EmployeeTO employee) {
        Employee emp = converter.convert(employee, Employee.class);
        employeeDAO.createEmp(emp);
        employee.setId(emp.getId());
    }

    @PreAuthorize("hasRole('employee_manager') or hasRole('superadmin')")
    @Override
    @Transactional
    public EmployeeTO retrieveEmp(Long id) {
        Employee emp = employeeDAO.retrieveEmp(id);
        EmployeeTO employee = converter.convert(emp, EmployeeTO.class);
        return employee;
    }

    @PreAuthorize("hasRole('employee_manager') or hasRole('superadmin')")
    @Override
    @Transactional
    public void updateEmp(EmployeeTO employee) {
        Employee emp = converter.convert(employee, Employee.class);
        employeeDAO.updateEmp(emp);
    }

    @PreAuthorize("hasRole('employee_manager') or hasRole('superadmin')")
    @Override
    @Transactional
    public void deleteEmp(EmployeeTO employee) {
        Employee emp = converter.convert(employee, Employee.class);
        employeeDAO.deleteEmp(emp);
    }

    @PreAuthorize("hasRole('employee_manager') or hasRole('superadmin')")
    @Override
    @Transactional
    public List<EmployeeTO> retrieveAll() {
        List<Employee> emp = employeeDAO.retrieveAll();
        List<EmployeeTO> employee = new LinkedList<EmployeeTO>();
        for(Employee e : emp)
        {
            employee.add(converter.convert(e, EmployeeTO.class));
        }
        return employee;
    }

    @PreAuthorize("hasRole('employee_manager') or hasRole('superadmin')")
    @Override
    @Transactional
    public List<EmployeeTO> retrieveByCategory(EmployeePosition position) {
        List<Employee> emp = employeeDAO.retrieveByCategory(position);
        List<EmployeeTO> employee = new LinkedList<EmployeeTO>();
        for(Employee e : emp)
        {
            employee.add(converter.convert(e, EmployeeTO.class));
        }
        return employee;
    }

    public void setEmployeeDAO(EmployeeDAO employeeDAO) {
        this.employeeDAO = employeeDAO;
    }

    public void setConverter(ConversionService converter) {
        this.converter = converter;
    }
}
